<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img1.baidu.com/it/u=3788617063,3371352907&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >django路由层</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、路由匹配"><a href="#一、路由匹配" class="headerlink" title="一、路由匹配"></a>一、路由匹配</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是路由匹配</span><br><span class="line">	url(<span class="string">r&quot;test&quot;</span>, views.test)</span><br><span class="line">	url(<span class="string">r&quot;testadd&quot;</span>, views.test)</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    这就是路由匹配</span></span><br><span class="line"><span class="string">    url的第一个其实是正则表达式</span></span><br><span class="line"><span class="string">    	只要第一个正则表达式内容匹配到了就不会往下匹配，直接执行视图函数</span></span><br><span class="line"><span class="string">    	所以上述代码中的testadd其实是匹配不到的</span></span><br><span class="line"><span class="string">    当你输入url发现自动添加斜杠的时候</span></span><br><span class="line"><span class="string">    	其实是Django帮你添加的</span></span><br><span class="line"><span class="string">    	Django会在内部进行重定向</span></span><br><span class="line"><span class="string">    	一次不行，加了斜杠再来一次</span></span><br><span class="line"><span class="string">    	当然也可以取消</span></span><br><span class="line"><span class="string">    	只要在配置文件里面增加一条代码</span></span><br><span class="line"><span class="string">    	APPEND_SLASH = False  默认是True</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"><span class="number">2.</span> 解决办法</span><br><span class="line">	urlpatterns = [</span><br><span class="line">    url(<span class="string">r&#x27;^admin/&#x27;</span>, admin.site.urls),</span><br><span class="line">    <span class="comment"># 首页</span></span><br><span class="line">    url(<span class="string">r&#x27;^$&#x27;</span>,views.home),</span><br><span class="line">    <span class="comment"># 路由匹配</span></span><br><span class="line">    url(<span class="string">r&#x27;^test/$&#x27;</span>,views.test),</span><br><span class="line">    url(<span class="string">r&#x27;^testadd/$&#x27;</span>,views.testadd),</span><br><span class="line">    <span class="comment"># 尾页(了解)</span></span><br><span class="line">    url(<span class="string">r&#x27;&#x27;</span>,views.error),</span><br><span class="line">	]</span><br><span class="line"> 	<span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string"> 	因为第一个其实是正则表达式 所以我们加限制条件^/$即可以</span></span><br><span class="line"><span class="string"> 	而且我们在启动Django后 进入页面就是报错页面 很不爽 所以我们也可以加个首页URL</span></span><br><span class="line"><span class="string"> 	尾页URL则是用来专门当做报错页面，但是方法有很多种 所以我们只要了解尾页即可</span></span><br><span class="line"><span class="string"> 	&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="二、无名分组"><a href="#二、无名分组" class="headerlink" title="二、无名分组"></a>二、无名分组</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是分组</span><br><span class="line">	分组其实就是讲正则表达式括号括起来</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span> 无名分组代码演示</span><br><span class="line">    url(<span class="string">r&#x27;^test/(\d+)/&#x27;</span>,views.test)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">test</span>(<span class="params">request,xx</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(xx)</span><br><span class="line">        <span class="keyword">return</span> HttpResponse(<span class="string">&#x27;test&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">无名分组其实就是将括号内的正则表达式匹配到的内容当做位置参数传到视图函数去</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="三、有名分组"><a href="#三、有名分组" class="headerlink" title="三、有名分组"></a>三、有名分组</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是有名分组</span><br><span class="line">	有名分组其实就是讲括号内的正则表达式匹配到的内容以关键字参数传到视图函数去</span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">	可以给正则表达式起个别名</span><br><span class="line">    url(<span class="string">r&#x27;^testadd/(?p&lt;year&gt;\d+)&#x27;</span>,views.testadd)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">testadd</span>(<span class="params">request,year</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(year)</span><br><span class="line">        <span class="keyword">return</span> HttpResponse(<span class="string">&#x27;testadd&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="四、分析无名有名分组"><a href="#四、分析无名有名分组" class="headerlink" title="四、分析无名有名分组"></a>四、分析无名有名分组</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 无名和有名分组是否可以一块用呢</span><br><span class="line">	直接给你答案 不能</span><br><span class="line">    但是一个分组可以用很多次</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span> 代码演示(用很多次)</span><br><span class="line">	url(<span class="string">r&#x27;^index/(\d+)/(\d+)/(\d+)/&#x27;</span>,views.index),</span><br><span class="line">	url(<span class="string">r&#x27;^index/(?P&lt;year&gt;\d+)/(?P&lt;age&gt;\d+)/(?P&lt;month&gt;\d+)/&#x27;</span>,views.index),</span><br></pre></td></tr></table></figure>

<h2 id="五、反向解析"><a href="#五、反向解析" class="headerlink" title="五、反向解析"></a>五、反向解析</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是反向解析</span><br><span class="line">	通过一些方法得到一个结果 该结果可以直接访问对应的url触发视图函数</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">    <span class="comment"># 先给路由与视图函数起一个别名</span></span><br><span class="line">        url(<span class="string">r&#x27;^func_kkk/&#x27;</span>,views.func,name=<span class="string">&#x27;ooo&#x27;</span>)</span><br><span class="line">    <span class="comment"># 反向解析</span></span><br><span class="line">        <span class="comment"># 后端反向解析</span></span><br><span class="line">        <span class="keyword">from</span> django.shortcuts <span class="keyword">import</span> render,HttpResponse,redirect,reverse</span><br><span class="line">        reverse(<span class="string">&#x27;ooo&#x27;</span>)</span><br><span class="line">      <span class="comment"># 前端反向解析</span></span><br><span class="line">        &lt;a href=<span class="string">&quot;&#123;% url &#x27;ooo&#x27; %&#125;&quot;</span>&gt;<span class="number">111</span>&lt;/a&gt;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">别名不能出现冲突!!!</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="六、无名有名反向解析"><a href="#六、无名有名反向解析" class="headerlink" title="六、无名有名反向解析"></a>六、无名有名反向解析</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>什么是无名反向解析(直接上代码演示)</span><br><span class="line">    url(<span class="string">r&#x27;^index/(\d+)/&#x27;</span>, views.index, name=<span class="string">&#x27;my_index&#x27;</span>)</span><br><span class="line">    <span class="comment"># 后端写法</span></span><br><span class="line">    reverse(<span class="string">&#x27;my_index&#x27;</span>, args=(<span class="number">1</span>,))</span><br><span class="line">    <span class="comment"># 前端写法</span></span><br><span class="line">    &#123;% url <span class="string">&#x27;mi_index&#x27;</span> <span class="number">1</span> %&#125;</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">这个数字我们应该填什么 推荐是这个数据的主键，所以这个反向解析可以用于数据的编辑删除等</span></span><br><span class="line"><span class="string">	url(r&#x27;^edit/(\d+)/&#x27;,views.edit,name=&#x27;xxx&#x27;)</span></span><br><span class="line"><span class="string">	</span></span><br><span class="line"><span class="string">	def edit(request,edit_id):</span></span><br><span class="line"><span class="string">		reverse(&#x27;xxx&#x27;,args=(edit_id,))</span></span><br><span class="line"><span class="string">		</span></span><br><span class="line"><span class="string">	&#123;%for user_obj in user_queryset%&#125;</span></span><br><span class="line"><span class="string">		&lt;a href=&quot;&#123;% url &#x27;xxx&#x27; user_obj.id %&#125;&quot;&gt;编辑&lt;/a&gt;</span></span><br><span class="line"><span class="string">	&#123;%endfor%&#125;</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 什么是有名反向解析(上代码)</span><br><span class="line">   url(<span class="string">r&#x27;^func/(?P&lt;year&gt;\d+)/&#x27;</span>,views.func,name=<span class="string">&#x27;ooo&#x27;</span>)</span><br><span class="line">   <span class="number">2.1</span> 前端</span><br><span class="line">		<span class="comment"># 了解		</span></span><br><span class="line">        &lt;a href=<span class="string">&quot;&#123;% url &#x27;ooo&#x27; year=123 %&#125;&quot;</span>&gt;<span class="number">111</span>&lt;/a&gt;  </span><br><span class="line">        <span class="comment"># 记忆</span></span><br><span class="line">        &lt;a href=<span class="string">&quot;&#123;% url &#x27;ooo&#x27; 123 %&#125;&quot;</span>&gt;<span class="number">222</span>&lt;/a&gt;  			</span><br><span class="line"></span><br><span class="line">   <span class="number">2.2</span> 后端	</span><br><span class="line">       <span class="comment"># 有名分组反向解析 写法1  了解</span></span><br><span class="line">       <span class="built_in">print</span>(reverse(<span class="string">&#x27;ooo&#x27;</span>,kwargs=&#123;<span class="string">&#x27;year&#x27;</span>:<span class="number">123</span>&#125;))</span><br><span class="line">       <span class="comment"># 简便的写法  减少你的脑容量消耗 记跟无名一样的操作即可</span></span><br><span class="line">       <span class="built_in">print</span>(reverse(<span class="string">&#x27;ooo&#x27;</span>,args=(<span class="number">111</span>,)))</span><br></pre></td></tr></table></figure>

<h2 id="七、路由分配"><a href="#七、路由分配" class="headerlink" title="七、路由分配"></a>七、路由分配</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是路由分配</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    django的每一个应用都可以有自己的templates文件夹 urls.py static文件夹</span></span><br><span class="line"><span class="string">    正是基于上述的特点 django能够非常好的做到分组开发(每个人只写自己的app)</span></span><br><span class="line"><span class="string">    作为组长 只需要将手下书写的app全部拷贝到一个新的django项目中 然后在配置文件里面注册所有的app再利用路由分发的特点将所有的		app整合起来</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    当一个django项目中的url特别多的时候 总路由urls.py代码非常冗余不好维护</span></span><br><span class="line"><span class="string">    这个时候也可以利用路由分发来减轻总路由的压力</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    利用路由分发之后 总路由不再干路由与视图函数的直接对应关系</span></span><br><span class="line"><span class="string">    而是做一个分发处理</span></span><br><span class="line"><span class="string">    识别当前url是属于哪个应用下的 直接分发给对应的应用去处理</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">    <span class="comment"># 总路由</span></span><br><span class="line">    	<span class="keyword">from</span> django.conf.urls <span class="keyword">import</span> url, include</span><br><span class="line">        <span class="keyword">from</span> app01 <span class="keyword">import</span> urls <span class="keyword">as</span> app01_urls</span><br><span class="line">        <span class="keyword">from</span> app02 <span class="keyword">import</span> urls <span class="keyword">as</span> app02_urls</span><br><span class="line">        urlpatterns = [</span><br><span class="line">            url(<span class="string">r&#x27;^admin/&#x27;</span>, admin.site.urls),</span><br><span class="line">            <span class="comment"># 1.路由分发</span></span><br><span class="line">            url(<span class="string">r&#x27;^app01/&#x27;</span>,include(app01_urls)),  <span class="comment"># 只要url前缀是app01开头 全部交给app01处理</span></span><br><span class="line">            url(<span class="string">r&#x27;^app02/&#x27;</span>,include(app02_urls))   <span class="comment"># 只要url前缀是app02开头 全部交给app02处理</span></span><br><span class="line"></span><br><span class="line">            <span class="comment"># 2.终极写法  推荐使用</span></span><br><span class="line">            url(<span class="string">r&#x27;^app01/&#x27;</span>,include(<span class="string">&#x27;app01.urls&#x27;</span>)),</span><br><span class="line">            url(<span class="string">r&#x27;^app02/&#x27;</span>,include(<span class="string">&#x27;app02.urls&#x27;</span>))</span><br><span class="line">            <span class="comment"># 注意事项:总路由里面的url千万不能加$结尾</span></span><br><span class="line">        ]</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 子路由</span></span><br><span class="line">      <span class="comment"># app01 urls.py</span></span><br><span class="line">          <span class="keyword">from</span> django.conf.urls <span class="keyword">import</span> url</span><br><span class="line">          <span class="keyword">from</span> app01 <span class="keyword">import</span> views</span><br><span class="line"></span><br><span class="line">          urlpatterns = [</span><br><span class="line">              url(<span class="string">r&#x27;^reg/&#x27;</span>,views.reg)</span><br><span class="line">          ]</span><br><span class="line">      <span class="comment"># app02 urls.py</span></span><br><span class="line">          <span class="keyword">from</span> django.conf.urls <span class="keyword">import</span> url</span><br><span class="line">          <span class="keyword">from</span> app02 <span class="keyword">import</span> views</span><br><span class="line"></span><br><span class="line">          urlpatterns = [</span><br><span class="line">              url(<span class="string">r&#x27;^reg/&#x27;</span>,views.reg)</span><br><span class="line">          ]</span><br></pre></td></tr></table></figure>

<h2 id="八、名称空间"><a href="#八、名称空间" class="headerlink" title="八、名称空间"></a>八、名称空间</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 为什么用名称空间</span><br><span class="line">	在前面我们已经详细的讲了什么是名称空间，在这里我们直接讲为什么要用名称空间</span><br><span class="line">    因为有路由分配我们就可以多人同时进行工作，但是我们取得变量名重复了怎么办</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span>当多个应用出现了相同的别名 我们研究反向解析会不会自动识别应用前缀</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    正常情况下的反向解析是没有办法自动识别前缀的</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">3.</span>名称空间 代码演示</span><br><span class="line">	<span class="comment"># 总路由</span></span><br><span class="line">    url(<span class="string">r&#x27;^app01/&#x27;</span>,include(<span class="string">&#x27;app01.urls&#x27;</span>,namespace=<span class="string">&#x27;app01&#x27;</span>)),</span><br><span class="line">    url(<span class="string">r&#x27;^app02/&#x27;</span>,include(<span class="string">&#x27;app02.urls&#x27;</span>,namespace=<span class="string">&#x27;app02&#x27;</span>))</span><br><span class="line">    <span class="comment"># 解析的时候</span></span><br><span class="line">  	<span class="comment"># app01</span></span><br><span class="line">  	urlpatterns = [</span><br><span class="line">    url(<span class="string">r&#x27;^reg/&#x27;</span>,views.reg,name=<span class="string">&#x27;reg&#x27;</span>)</span><br><span class="line">		]</span><br><span class="line">    <span class="comment"># app02</span></span><br><span class="line">    urlpatterns = [</span><br><span class="line">    url(<span class="string">r&#x27;^reg/&#x27;</span>,views.reg,name=<span class="string">&#x27;reg&#x27;</span>)</span><br><span class="line">		]</span><br><span class="line">    </span><br><span class="line">  	reverse(<span class="string">&#x27;app01:reg&#x27;</span>)</span><br><span class="line">    reverse(<span class="string">&#x27;app02:reg&#x27;</span>)</span><br><span class="line">    </span><br><span class="line">    &#123;% url <span class="string">&#x27;app01:reg&#x27;</span> %&#125;</span><br><span class="line">    &#123;% url <span class="string">&#x27;app02:reg&#x27;</span> %&#125;</span><br><span class="line">    <span class="comment"># 其实只要保证名字不冲突 就没有必要使用名称空间</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    一般情况下 有多个app的时候我们在起别名的时候会加上app的前缀</span></span><br><span class="line"><span class="string">    这样的话就能够确保多个app之间名字不冲突的问题</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    urlpatterns = [</span><br><span class="line">        url(<span class="string">r&#x27;^reg/&#x27;</span>,views.reg,name=<span class="string">&#x27;app01_reg&#x27;</span>)</span><br><span class="line">    ]</span><br><span class="line">    urlpatterns = [</span><br><span class="line">        url(<span class="string">r&#x27;^reg/&#x27;</span>,views.reg,name=<span class="string">&#x27;app02_reg&#x27;</span>)</span><br><span class="line">    ]</span><br><span class="line">    </span><br></pre></td></tr></table></figure>

<h2 id="九、伪静态"><a href="#九、伪静态" class="headerlink" title="九、伪静态"></a>九、伪静态</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是伪静态</span><br><span class="line">	将一个动态网页伪装成静态网页</span><br><span class="line"><span class="number">2.</span> 什么是静态网页</span><br><span class="line">	数据是写死的 万年不变</span><br><span class="line"><span class="number">3.</span> 为什么要用伪静态</span><br><span class="line">		伪装的目的在于增大本网站的seo查询力度</span><br><span class="line">		并且增加搜索引擎收藏本网上的概率</span><br><span class="line">	</span><br><span class="line">	搜索引擎本质上就是一个巨大的爬虫程序</span><br><span class="line">	</span><br><span class="line">	总结:</span><br><span class="line">		无论你怎么优化 怎么处理</span><br><span class="line">		始终还是干不过RMB玩家</span><br><span class="line"><span class="number">4.</span> 伪静态的代码演示</span><br><span class="line">    urlpatterns = [</span><br><span class="line">        url(<span class="string">r&#x27;^reg.html&#x27;</span>,views.reg,name=<span class="string">&#x27;app02_reg&#x27;</span>)</span><br><span class="line">    ]</span><br></pre></td></tr></table></figure>

<h2 id="十、虚拟环境"><a href="#十、虚拟环境" class="headerlink" title="十、虚拟环境"></a>十、虚拟环境</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">在正常开发中 我们会给每一个项目配备一个该项目独有的解释器环境</span></span><br><span class="line"><span class="string">该环境内只有该项目用到的模块 用不到一概不装</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">linux:缺什么才装什么</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">虚拟环境</span></span><br><span class="line"><span class="string">	你每创建一个虚拟环境就类似于重新下载了一个纯净的python解释器</span></span><br><span class="line"><span class="string">	但是虚拟环境不要创建太多，是需要消耗硬盘空间的</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">扩展:</span></span><br><span class="line"><span class="string">	每一个项目都需要用到很多模块 并且每个模块版本可能还不一样</span></span><br><span class="line"><span class="string">	那我该如何安装呢？ 一个个看一个个装？？？</span></span><br><span class="line"><span class="string">	</span></span><br><span class="line"><span class="string">	开发当中我们会给每一个项目配备一个requirements.txt文件</span></span><br><span class="line"><span class="string">	里面书写了该项目所有的模块即版本</span></span><br><span class="line"><span class="string">	你只需要直接输入一条命令即可一键安装所有模块即版本</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="十一、Django版本区别"><a href="#十一、Django版本区别" class="headerlink" title="十一、Django版本区别"></a>十一、Django版本区别</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;我们将会对比django 1.x 2.x 3.x版本的区别&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> 第一种区别</span><br><span class="line">    <span class="number">1.</span>x 的路由层用的是url方法</span><br><span class="line">    <span class="number">2.</span>x &amp; <span class="number">3.</span>x则使用的是path方法</span><br><span class="line">   	path方法第一个参数是不支持正则表达式的</span><br><span class="line">    url方法则是支持</span><br><span class="line">    也正是因为这点所以<span class="number">2.</span>x <span class="number">3.</span>x 给了一个re_path方法 </span><br><span class="line">    re_path第一个参数是支持正则表达式的</span><br><span class="line">    <span class="number">2.</span>x &amp; <span class="number">3.</span>x里面的re_path相当于<span class="number">1.</span>x的url</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span>虽然path不支持正则 但是它的内部支持五种转换器</span><br><span class="line">	path(<span class="string">&#x27;index/&lt;int:id&gt;/&#x27;</span>,index)</span><br><span class="line">	<span class="comment"># 将第二个路由里面的内容先转成整型然后以关键字的形式传递给后面的视图函数</span></span><br><span class="line">	<span class="function"><span class="keyword">def</span> <span class="title">index</span>(<span class="params">request,<span class="built_in">id</span></span>):</span></span><br><span class="line">         <span class="built_in">print</span>(<span class="built_in">id</span>,<span class="built_in">type</span>(<span class="built_in">id</span>))</span><br><span class="line">         <span class="keyword">return</span> HttpResponse(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">    </span><br><span class="line"> </span><br><span class="line">  </span><br><span class="line">   	<span class="built_in">str</span>,匹配除了路径分隔符（/）之外的非空字符串，这是默认的形式</span><br><span class="line">	<span class="built_in">int</span>,匹配正整数，包含<span class="number">0</span>。</span><br><span class="line">	slug,匹配字母、数字以及横杠、下划线组成的字符串。</span><br><span class="line">	uuid,匹配格式化的uuid，如 075194d3-<span class="number">6885</span>-417e-a8a8-6c931e272f00。</span><br><span class="line">	path,匹配任何非空字符串，包含了路径分隔符（/）（不能用？）</span><br><span class="line">	</span><br><span class="line"><span class="number">3.</span>除了有默认的五个转换器之外 还支持自定义转换器(了解)</span><br><span class="line">	<span class="class"><span class="keyword">class</span> <span class="title">MonthConverter</span>:</span></span><br><span class="line">    regex=<span class="string">&#x27;\d&#123;2&#125;&#x27;</span> <span class="comment"># 属性名必须为regex</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">to_python</span>(<span class="params">self, value</span>):</span></span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">int</span>(value)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">to_url</span>(<span class="params">self, value</span>):</span></span><br><span class="line">        <span class="keyword">return</span> value <span class="comment"># 匹配的regex是两个数字，返回的结果也必须是两个数字</span></span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">from</span> django.urls <span class="keyword">import</span> path,register_converter</span><br><span class="line">	<span class="keyword">from</span> app01.path_converts <span class="keyword">import</span> MonthConverter</span><br><span class="line"></span><br><span class="line">	<span class="comment"># 先注册转换器</span></span><br><span class="line">	register_converter(MonthConverter,<span class="string">&#x27;mon&#x27;</span>)</span><br><span class="line"></span><br><span class="line">	<span class="keyword">from</span> app01 <span class="keyword">import</span> views</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	urlpatterns = [</span><br><span class="line">    path(<span class="string">&#x27;articles/&lt;int:year&gt;/&lt;mon:month&gt;/&lt;slug:other&gt;/&#x27;</span>, 	views.article_detail, name=<span class="string">&#x27;aaa&#x27;</span>),</span><br><span class="line"></span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"><span class="number">4.</span>第二种区别</span><br><span class="line">	模型层里面<span class="number">1.</span>X外键默认都是级联更新删除的</span><br><span class="line">	但是到了<span class="number">2.</span>X和<span class="number">3.</span>X中需要你自己手动配置参数</span><br><span class="line">	models.ForeignKey(to=<span class="string">&#x27;Publish&#x27;</span>)</span><br><span class="line">	</span><br><span class="line">	models.ForeignKey(to=<span class="string">&#x27;Publish&#x27;</span>,on_delete=models.CASCADE...)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>




            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/02/Django------%E8%B7%AF%E7%94%B1%E5%B1%82/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/02/Django------%E8%B7%AF%E7%94%B1%E5%B1%82/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
